package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.configure.Result;
import com.store.entity.Message;
import com.store.mapper.MessageMapper;
import com.store.service.MessageService;
import com.store.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-01-06
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    MessageMapper messageMapper;

    @Override
    public Result<Message> updateMessage(Message message) {
        Result<Message> result = new Result<>();
        try {
            if (StringUtils.isEmpty(message.getMessageId())) {
                //如果ID为空，代表添加
                message.setMessageId(IDGenerator.getUUID("MESSAGE"));
                message.setCreateTime(new Date());
                message.setUpdateTime(new Date());
                messageMapper.insert(message);
            } else {
                //如果ID不为空，代表修改
                message.setUpdateTime(new Date());
                messageMapper.updateById(message);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Message> delMessage(Message message) {
        Result<Message> result = new Result<>();
        try {
            if (StringUtils.isEmpty(message.getMessageId())) {
                result.setMessage("messageId can't be empty");
                return result;
            }
            messageMapper.deleteById(message.getMessageId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Message> batchDelMessage(Message message) {
        Result<Message> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(message.getIds())) {
                String[] ids = message.getIds().split(",");
                for (String id : ids) {
                    messageMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Message> queryMessage(Message message) {
        Result<Message> result = new Result<>();
        try {
            QueryWrapper<Message> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(message.getStoreId())) {
                wrapper.lambda().eq(Message::getStoreId, message.getStoreId());
            }
            if (!StringUtils.isEmpty(message.getUserId())) {
                wrapper.lambda().eq(Message::getUserId, message.getUserId());
            }
            if (!StringUtils.isEmpty(message.getResponseUserId())) {
                wrapper.lambda().eq(Message::getResponseUserId, message.getResponseUserId());
            }
            //查询条件拼接，仅限单表查询可用
            List<Message> messages = messageMapper.selectList(wrapper);
            result.setReturnList(messages);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Message> queryMessageByPage(Message message) {
        Result<Message> result = new Result<>();

        try {
            if (message.getCurrent() == null || message.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Message> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(message.getStoreId())) {
                wrapper.lambda().eq(Message::getStoreId, message.getStoreId());
            }
            if (!StringUtils.isEmpty(message.getUserId())) {
                wrapper.lambda().eq(Message::getUserId, message.getUserId());
            }
            if (!StringUtils.isEmpty(message.getResponseUserId())) {
                wrapper.lambda().eq(Message::getResponseUserId, message.getResponseUserId());
            }

            IPage<Message> messages = messageMapper.selectPage(new Page<>(message.getCurrent(), message.getSize()), wrapper);
            result.setReturnPage(messages);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Message> queryMessageById(Message message) {
        Result<Message> result = new Result<>();
        try {
            Message returnMessage = messageMapper.selectById(message.getMessageId());
            result.setReturnObj(returnMessage);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
